package com.dhcc.bpm.modules.scheduler.service.Impl;

import com.dhcc.bpm.common.utils.EmailUtils;
import com.dhcc.bpm.modules.orga.entity.Orga;
import com.dhcc.bpm.modules.orga.mapper.OrgaMapper;
import com.dhcc.bpm.modules.scheduler.consoletable.ConsoleTable;
import com.dhcc.bpm.modules.scheduler.consoletable.enums.Align;
import com.dhcc.bpm.modules.scheduler.consoletable.table.Cell;
import com.dhcc.bpm.modules.scheduler.mapper.GatewayLogDataMapper;
import com.dhcc.bpm.modules.scheduler.service.ReportService;
import com.dhcc.bpm.modules.scheduler.vo.ReportApiVo;
import com.dhcc.bpm.modules.scheduler.vo.ReportAppVo;
import com.dhcc.bpm.modules.scheduler.vo.ReportGateWayVo;
import com.dhcc.bpm.modules.scheduler.vo.ReportProductVo;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 报表推送实现
 */
@Service
@AllArgsConstructor
@NoArgsConstructor
public class ReportServiceImpl implements ReportService {

    @Autowired
    private EmailUtils emailUtils;

    @Autowired
    private OrgaMapper orgaMapper;

    @Autowired
    private GatewayLogDataMapper gatewayLogDataMapper;

    //报表接收人邮箱
    @Value("${scheduler.report.mailAddress}")
    private String list;

    @Override
    public void dayReport() {

//        String list = "wangtao41@ceair.com";
        StringBuilder content=new StringBuilder("本日：最新接入机构");
        content.append("<br/>");
        String mailTitle = "最新接入机构";
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sf.format(new Date());
        List<Orga> orgas = orgaMapper.getOrgaList(date);
//        List<Orga> orgas = orgaMapper.selectList(Wrappers.<Orga>lambdaQuery()
//                .eq(Orga::getAuthFlag,1)
//                .eq(Orga::getOperationType,1)
//                .apply("DATE_FORMAT( access_time, '%Y-%m-%d' ) = DATE_FORMAT( '"+new Date()+"', '%Y-%m-%d' )")
//                .orderByDesc(Orga::getAccessTime)
//                .last("limit 0,5"));
        if(orgas.size()==0){
            content.append("本日无新接入机构");
        }else{
            List<Cell> header = new ArrayList<Cell>();
            header.add(new Cell(Align.CENTER,"序号"));
            header.add(new Cell(Align.CENTER,"机构名称"));
//          add(new Cell(Align.CENTER,"组织机构代码"));
//          add(new Cell(Align.CENTER,"机构认证状态"));
//          add(new Cell(Align.CENTER,"接入时间"));
            header.add(new Cell(Align.CENTER,"负责人"));
//          add(new Cell(Align.CENTER,"负责人电话"));

            List<List<Cell>> body = new ArrayList<List<Cell>>(){{}};
            for (int i = 0; i < orgas.size(); i++) {
                ArrayList<Cell> cells = new ArrayList<Cell>();
                cells.add(new Cell(Align.CENTER,String.valueOf(i + 1) ));
                cells.add(new Cell(Align.CENTER,  orgas.get(i).getOrgaName().length() <= 5 ? orgas.get(i).getOrgaName() : orgas.get(i).getOrgaName().substring(0,5)));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getOrgaCode()));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getAuthFlag()));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getAccessTime().toString()));
                cells.add(new Cell(Align.CENTER,orgas.get(i).getPerCharge()));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getPerNumber()));
                body.add(cells);
            }
            content.append(new ConsoleTable.ConsoleTableBuilder().addHeaders(header).addRows(body).build().getContent());
        }
        emailUtils.sendEmail(mailTitle,list,mailTitle,content.toString());

        StringBuilder content1=new StringBuilder("本日：机构交易量排名");
        content1.append("<br/>");
        String mailTitle1 = "机构交易量本日排名";
        List<Cell> header1 = new ArrayList<Cell>();
        header1.add(new Cell(Align.CENTER,"排名"));
        header1.add(new Cell(Align.CENTER,"机构名称"));
        header1.add(new Cell(Align.CENTER,"交易量(笔)"));
        header1.add(new Cell(Align.CENTER,"交易成功率"));
        header1.add(new Cell(Align.CENTER,"平均响应时间"));
        header1.add(new Cell(Align.CENTER,"本日涨幅"));
//            header1.add(new Cell(Align.CENTER,"组织结构代码"));
//            header1.add(new Cell(Align.CENTER,"接入时间"));
//            header1.add(new Cell(Align.CENTER,"负责人"));
//            header1.add(new Cell(Align.CENTER,"联系电话"));

        List<ReportGateWayVo> gatewayDayLog = gatewayLogDataMapper.getGatewayDayLog();
        List<List<Cell>> body1 = new ArrayList<List<Cell>>(){{}};
        for (int i = 0; i < gatewayDayLog.size(); i++) {
            ArrayList<Cell> cells = new ArrayList<Cell>();
            cells.add(new Cell(Align.CENTER,String.valueOf(i + 1) ));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getOrgaName().length() <= 5 ? gatewayDayLog.get(i).getOrgaName(): gatewayDayLog.get(i).getOrgaName().substring(0,5)));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getNum()));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getSuccessRate()));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getResponseTime()));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getIncrease()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getOpcOrgaCode()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getRegTime()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getPerCharge()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getPerNumber()));
            body1.add(cells);
        }

        content1.append(new ConsoleTable.ConsoleTableBuilder().addHeaders(header1).addRows(body1).build().getContent());
        emailUtils.sendEmail(mailTitle1,list,mailTitle1,content1.toString());

        StringBuilder content2=new StringBuilder("本日：渠道总交易量排名TOP5");
        content2.append("<br/>");
        String mailTitle2 = "渠道总交易量排名TOP5";

        List<Cell> header2 = new ArrayList<Cell>();
        header2.add(new Cell(Align.CENTER,"排名"));
        header2.add(new Cell(Align.CENTER,"渠道名称"));
        header2.add(new Cell(Align.CENTER,"交易量(笔)"));
        header2.add(new Cell(Align.CENTER,"交易成功率"));
        header2.add(new Cell(Align.CENTER,"平均响应时间"));

        List<ReportAppVo> appIncrease = gatewayLogDataMapper.getAppIncrease();
        List<List<Cell>> body2 = new ArrayList<List<Cell>>(){{}};
        for (int i = 0; i < appIncrease.size(); i++) {
            ArrayList<Cell> cells = new ArrayList<Cell>();
            cells.add(new Cell(Align.CENTER, String.valueOf(i + 1)));
            cells.add(new Cell(Align.CENTER, appIncrease.get(i).getAppName().length() <= 5 ? appIncrease.get(i).getAppName() : appIncrease.get(i).getAppName().substring(0, 5)));
            cells.add(new Cell(Align.CENTER, appIncrease.get(i).getNum()));
            cells.add(new Cell(Align.CENTER, appIncrease.get(i).getSuccessRate()));
            cells.add(new Cell(Align.CENTER, appIncrease.get(i).getResponseTime()));
            body2.add(cells);
        }
        content2.append(new ConsoleTable.ConsoleTableBuilder().addHeaders(header2).addRows(body2).build().getContent());
        emailUtils.sendEmail(mailTitle2,list,mailTitle2,content2.toString());

        StringBuilder content3=new StringBuilder("本日：产品总交易量排名TOP5");
        content3.append("<br/>");
        String mailTitle3 = "产品总交易量排名TOP5";
        List<Cell> header3 = new ArrayList<Cell>();
        header3.add(new Cell(Align.CENTER,"排名"));
        header3.add(new Cell(Align.CENTER,"产品名称"));
        header3.add(new Cell(Align.CENTER,"交易量(笔)"));
        header3.add(new Cell(Align.CENTER,"交易成功率"));
        header3.add(new Cell(Align.CENTER,"平均响应时间"));


        List<ReportProductVo> productIncrease = gatewayLogDataMapper.getProductIncrease();
        List<List<Cell>> body3 = new ArrayList<List<Cell>>(){{}};
        for (int i = 0; i < productIncrease.size(); i++) {
            ArrayList<Cell> cells = new ArrayList<Cell>();
            cells.add(new Cell(Align.CENTER,String.valueOf(i + 1) ));
            cells.add(new Cell(Align.CENTER,productIncrease.get(i).getProductName().length() <= 5 ? productIncrease.get(i).getProductName() : productIncrease.get(i).getProductName().substring(0,5)));
            cells.add(new Cell(Align.CENTER,productIncrease.get(i).getNum()));
            cells.add(new Cell(Align.CENTER,productIncrease.get(i).getSuccessRate()));
            cells.add(new Cell(Align.CENTER,productIncrease.get(i).getResponseTime()));
            body3.add(cells);
        }
        content3.append(new ConsoleTable.ConsoleTableBuilder().addHeaders(header3).addRows(body3).build().getContent());
        emailUtils.sendEmail(mailTitle3,list,mailTitle3,content3.toString());

        StringBuilder content4=new StringBuilder("本日：业务API总交易量排名TOP5");
        content4.append("<br/>");
        String mailTitle4 = "业务API总交易量排名TOP5";
        List<Cell> header4 = new ArrayList<Cell>();
        header4.add(new Cell(Align.CENTER,"排名"));
        header4.add(new Cell(Align.CENTER,"API名称"));
        header4.add(new Cell(Align.CENTER,"交易量金额"));
        header4.add(new Cell(Align.CENTER,"交易量(笔)"));
        header4.add(new Cell(Align.CENTER,"交易成功率"));
        header4.add(new Cell(Align.CENTER,"平均响应时间"));

        List<ReportApiVo> costApiIncrease = gatewayLogDataMapper.getCostApiIncrease();
        List<List<Cell>> body4 = new ArrayList<List<Cell>>(){{}};
        for (int i = 0; i < costApiIncrease.size(); i++) {
            ArrayList<Cell> cells = new ArrayList<Cell>();
            cells.add(new Cell(Align.CENTER,String.valueOf(i + 1) ));
            cells.add(new Cell(Align.CENTER,costApiIncrease.get(i).getApiName().length() <= 5 ? costApiIncrease.get(i).getApiName() : costApiIncrease.get(i).getApiName().substring(0,5)));
            cells.add(new Cell(Align.CENTER,costApiIncrease.get(i).getTxAmt()));
            cells.add(new Cell(Align.CENTER,costApiIncrease.get(i).getNum()));
            cells.add(new Cell(Align.CENTER,costApiIncrease.get(i).getSuccessRate()));
            cells.add(new Cell(Align.CENTER,costApiIncrease.get(i).getResponseTime()));
            body4.add(cells);
        }
        content4.append(new ConsoleTable.ConsoleTableBuilder().addHeaders(header4).addRows(body4).build().getContent());
        emailUtils.sendEmail(mailTitle4,list,mailTitle4,content4.toString());

    }

    @Override
    public void weekReport() {

//        String list = "wangtao41@ceair.com";
        /*StringBuilder content=new StringBuilder("本周：最新接入机构");
        content.append("<br/>");
        String mailTitle = "最新接入机构";
        List<Orga> orgas = orgaMapper.selectList(Wrappers.<Orga>lambdaQuery()
                .eq(Orga::getAuthFlag,1)
                .eq(Orga::getOperationType,1)
                .apply("DATE_FORMAT( access_time, '%Y-%m-%d' ) " +" >= DATE_FORMAT( '"+ DateUtil.minusDays(new Date(), 6)+"', '%Y-%m-%d' )")
                .apply("DATE_FORMAT( access_time, '%Y-%m-%d' ) " +" <= DATE_FORMAT( '"+new Date()+"', '%Y-%m-%d' )")
                .orderByDesc(Orga::getAccessTime));
        if(orgas.size()==0){
            content.append("<br/>");
            content.append("本周无新接入机构");
        }else{
            List<Cell> header = new ArrayList<Cell>(){{
                add(new Cell(Align.CENTER,"序号"));
                add(new Cell(Align.CENTER,"机构名称"));
//                add(new Cell(Align.CENTER,"组织机构代码"));
//                add(new Cell(Align.CENTER,"机构认证状态"));
//                add(new Cell(Align.CENTER,"接入时间"));
                add(new Cell(Align.CENTER,"负责人"));
//                add(new Cell(Align.CENTER,"负责人电话"));
            }};
            List<List<Cell>> body = new ArrayList<List<Cell>>(){{}};
            for (int i = 0; i < orgas.size(); i++) {
                ArrayList<Cell> cells = new ArrayList<Cell>() {{
                }};
                cells.add(new Cell(Align.CENTER,String.valueOf(i + 1) ));
                cells.add(new Cell(Align.CENTER,orgas.get(i).getOrgaName().substring(0,5)));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getOrgaCode()));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getAuthFlag()));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getAccessTime().toString()));
                cells.add(new Cell(Align.CENTER,orgas.get(i).getPerCharge()));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getPerNumber()));
                body.add(cells);
            }
            content.append(new ConsoleTable.ConsoleTableBuilder().addHeaders(header).addRows(body).build().getContent());
        }
        emailUtils.sendEmail(mailTitle,list,mailTitle,content.toString());*/

        StringBuilder content1=new StringBuilder("本周：机构交易量排名");
        content1.append("<br/>");
        String mailTitle1 = "机构交易量本周排名";
        List<Cell> header1 = new ArrayList<Cell>();
        header1.add(new Cell(Align.CENTER,"排名"));
        header1.add(new Cell(Align.CENTER,"机构名称"));
        header1.add(new Cell(Align.CENTER,"交易量(笔)"));
        header1.add(new Cell(Align.CENTER,"交易成功率"));
        header1.add(new Cell(Align.CENTER,"平均响应时间"));
        header1.add(new Cell(Align.CENTER,"本日涨幅"));
//            add(new Cell(Align.CENTER,"组织结构代码"));
//            add(new Cell(Align.CENTER,"接入时间"));
//            add(new Cell(Align.CENTER,"负责人"));
//            add(new Cell(Align.CENTER,"联系电话"));


        List<ReportGateWayVo> gatewayDayLog = gatewayLogDataMapper.getGatewaySevenDayLog();
        List<List<Cell>> body1 = new ArrayList<List<Cell>>(){{}};
        for (int i = 0; i < gatewayDayLog.size(); i++) {
            ArrayList<Cell> cells = new ArrayList<Cell>();
            cells.add(new Cell(Align.CENTER,String.valueOf(i + 1) ));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getOrgaName().length() <= 5 ? gatewayDayLog.get(i).getOrgaName() : gatewayDayLog.get(i).getOrgaName().substring(0,5)));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getNum()));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getSuccessRate()));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getResponseTime()));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getIncrease()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getOpcOrgaCode()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getRegTime()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getPerCharge()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getPerNumber()));
            body1.add(cells);
        }

        content1.append(new ConsoleTable.ConsoleTableBuilder().addHeaders(header1).addRows(body1).build().getContent());
        emailUtils.sendEmail(mailTitle1,list,mailTitle1,content1.toString());

    }

    @Override
    public void monthReport() {

//        String list = "wangtao41@ceair.com";
        /*StringBuilder content=new StringBuilder("本月：最新接入机构");
        content.append("<br/>");
        String mailTitle = "最新接入机构";
        List<Orga> orgas = orgaMapper.selectList(Wrappers.<Orga>lambdaQuery()
                .eq(Orga::getAuthFlag,1)
                .eq(Orga::getOperationType,1)
                .apply("DATE_FORMAT( access_time, '%Y-%m' ) " +" = DATE_FORMAT( '"+new Date()+"', '%Y-%m' )")
                .orderByDesc(Orga::getAccessTime));
        if(orgas.size()==0){
            content.append("\n");
            content.append("本月无新接入机构");
        }else{
            List<Cell> header = new ArrayList<Cell>(){{
                add(new Cell(Align.CENTER,"序号"));
                add(new Cell(Align.CENTER,"机构名称"));
//                add(new Cell(Align.CENTER,"组织机构代码"));
//                add(new Cell(Align.CENTER,"机构认证状态"));
//                add(new Cell(Align.CENTER,"接入时间"));
                add(new Cell(Align.CENTER,"负责人"));
//                add(new Cell(Align.CENTER,"负责人电话"));
            }};
            List<List<Cell>> body = new ArrayList<List<Cell>>(){{}};
            for (int i = 0; i < orgas.size(); i++) {
                ArrayList<Cell> cells = new ArrayList<Cell>() {{
                }};
                cells.add(new Cell(Align.CENTER,String.valueOf(i + 1) ));
                cells.add(new Cell(Align.CENTER,orgas.get(i).getOrgaName().substring(0,5)));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getOrgaCode()));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getAuthFlag()));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getAccessTime().toString()));
                cells.add(new Cell(Align.CENTER,orgas.get(i).getPerCharge()));
//                cells.add(new Cell(Align.CENTER,orgas.get(i).getPerNumber()));
                body.add(cells);
            }
            content.append(new ConsoleTable.ConsoleTableBuilder().addHeaders(header).addRows(body).build().getContent());
        }
        emailUtils.sendEmail(mailTitle,list,mailTitle,content.toString());*/

        StringBuilder content1=new StringBuilder("近30日：机构交易量排名");
        content1.append("<br/>");
        String mailTitle1 = "机构交易量30日排名";
        List<Cell> header1 = new ArrayList<Cell>();
        header1.add(new Cell(Align.CENTER,"排名"));
        header1.add(new Cell(Align.CENTER,"机构名称"));
        header1.add(new Cell(Align.CENTER,"交易量(笔)"));
        header1.add(new Cell(Align.CENTER,"交易成功率"));
        header1.add(new Cell(Align.CENTER,"平均响应时间"));
        header1.add(new Cell(Align.CENTER,"本日涨幅"));
//            add(new Cell(Align.CENTER,"组织结构代码"));
//            add(new Cell(Align.CENTER,"接入时间"));
//            add(new Cell(Align.CENTER,"负责人"));
//            add(new Cell(Align.CENTER,"联系电话"));


        List<ReportGateWayVo> gatewayDayLog = gatewayLogDataMapper.getGatewayThirtyDayLog();
        List<List<Cell>> body1 = new ArrayList<List<Cell>>(){{}};
        for (int i = 0; i < gatewayDayLog.size(); i++) {
            ArrayList<Cell> cells = new ArrayList<Cell>();
            cells.add(new Cell(Align.CENTER,String.valueOf(i + 1) ));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getOrgaName().length() <= 5 ? gatewayDayLog.get(i).getOrgaName() : gatewayDayLog.get(i).getOrgaName().substring(0,5)));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getNum()));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getSuccessRate()));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getResponseTime()));
            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getIncrease()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getOpcOrgaCode()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getRegTime()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getPerCharge()));
//            cells.add(new Cell(Align.CENTER,gatewayDayLog.get(i).getPerNumber()));
            body1.add(cells);
        }

        content1.append(new ConsoleTable.ConsoleTableBuilder().addHeaders(header1).addRows(body1).build().getContent());
        emailUtils.sendEmail(mailTitle1,list,mailTitle1,content1.toString());
    }
}
